package com.mt.channel.service.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.channel.entity.ChannelCustomerEntity;
import com.mt.channel.mapper.ChannelCustomerEntityMapper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.common.util.ConvertUtil;

@Component
public class ChannelCustomerServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChannelCustomerServiceModel.class);

    @Autowired
    private ChannelCustomerEntityMapper channelCustomerEntityMapper;

    public ServiceResult<List<ChannelCustomerEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.channelCustomerEntityMapper, "Property 'channelCustomerEntityMapper' is required.");
        ServiceResult<List<ChannelCustomerEntity>> result = new ServiceResult<List<ChannelCustomerEntity>>();
        boolean withCustomerCourseCount = false;
        try {
            if ( params!=null&& params.containsKey("withCustomerCourseCount")) {
                withCustomerCourseCount = ConvertUtil.toBool(params.get("withCustomerCourseCount"), false);
            }
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }
            List<ChannelCustomerEntity> channelCustomerEntityList = this.channelCustomerEntityMapper.selectList(params);
            if (channelCustomerEntityList != null && withCustomerCourseCount) {
                Map<String, Object> relateParams = new HashMap<String, Object>();
                for (ChannelCustomerEntity channelCustomerEntity : channelCustomerEntityList) {
                    relateParams.put("customerId", channelCustomerEntity.getCustomerId());
                    relateParams.put("channelId", channelCustomerEntity.getChannelId());
                    int customerCourseCounts = channelCustomerEntityMapper.selectCourseCountsByCustomerId(relateParams);
                    channelCustomerEntity.setCustomerCourseCounts(customerCourseCounts);
                }
            }
            result.setResult(channelCustomerEntityList);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCustomerServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ChannelCustomerEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.channelCustomerEntityMapper, "Property 'channelCustomerEntityMapper' is required.");
        ServiceResult<ChannelCustomerEntity> result = new ServiceResult<ChannelCustomerEntity>();
        try {
            result.setResult(this.channelCustomerEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCustomerServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(ChannelCustomerEntity channelCustomerEntity) {
        Assert.notNull(this.channelCustomerEntityMapper, "Property 'channelCustomerEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelCustomerEntityMapper.insert(channelCustomerEntity);
            if (save > 0) {
                id = channelCustomerEntity.getChannelCustomerId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCustomerServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(ChannelCustomerEntity channelCustomerEntity) {
        Assert.notNull(this.channelCustomerEntityMapper, "Property 'channelCustomerEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelCustomerEntityMapper.updateByPrimaryKeySelective(channelCustomerEntity);
            if (save > 0) {
                id = channelCustomerEntity.getChannelCustomerId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCustomerServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(ChannelCustomerEntity channelCustomerEntity) {
        Assert.notNull(this.channelCustomerEntityMapper, "Property 'channelCustomerEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelCustomerEntityMapper.updateByPrimaryKeySelective(channelCustomerEntity);
            if (save > 0) {
                id = channelCustomerEntity.getChannelCustomerId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCustomerServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer channelCustomerId) {
        Assert.notNull(this.channelCustomerEntityMapper, "Property 'channelCustomerEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.channelCustomerEntityMapper.deleteByPrimaryKey(channelCustomerId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelCustomerServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
